//
//  StoryHeadView.m
//  MyItem
//
//  Created by SoulWater on 15/12/30.
//  Copyright © 2015年 admin. All rights reserved.
//

#import "StoryHeadView.h"
#import "UIButton+WebCache.h"
@implementation StoryHeadView
{
    UIPageControl *_pageControl;
}
-(void)awakeFromNib
{
    [super awakeFromNib];
    
    
    [self loadData];
    [self createView];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
        [self loadData];
        [self createView];
    }
    return self;
}


-(void)loadData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSDictionary *parametes = @{
                                @"sign": @"hGe6i2_oAMEgR9t7i0z6Pw",
                                @"d_model": @"iPhone4,1",
                                @"networkType": @"wifi",
                                @"adid": @"5BAD0863-7735-475F-B800-07AF74862C1F",
                                @"st": @"1451460885682",
                                @"body": @"{}",
                                @"osVersion": @"7.1.2",
                                @"d_brand": @"apple",
                                @"client": @"apple",
                                @"screen": @"640*960",
                                @"sv": @"1",
                                @"area": @"15_1213_3038_0",
                                @"build": @"35977",
                                @"uuid": @"hjudwgohxzVu96krv/T6Hg==",
                                @"clientVersion": @"4.4.3",
                                @"openudid": @"a1e609be3483012e09542a204a2a6dfd05c016be",
                                @"partner": @"apple"
                                };
    [manager POST:@"http://go.m.jd.com/client.action?functionId=getFirstPageNavigation" parameters:parametes success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject)
    {

//        NSLog(@"%@",responseObject);
        NSArray *bannerList = responseObject[@"bannerList"];
        NSMutableArray *bannerArray = [NSMutableArray array];
        NSArray *themeList = responseObject[@"themeList"];
        NSMutableArray *themeArray = [NSMutableArray array];
        for (NSDictionary *dic1 in bannerList) {
            
            NSString *str = dic1[@"bannerURL"];
            if (str != nil) {
                
                [bannerArray addObject:str];
            }
        }
        
        self.bannerArray = bannerArray;
//        NSLog(@"%@",_bannerArray);
        for (NSDictionary *dic2 in themeList) {
            
            NSString *string = dic2[@"firstPageImgURL"];
            [themeArray addObject:string];
        }
        self.themeArray = themeArray;
//        NSLog(@"%@",_themeArray);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}
-(void)setBannerArray:(NSArray *)bannerArray
{
    
    if (_bannerArray != bannerArray) {
        
        _bannerArray = bannerArray;

        _scrollView.contentSize = CGSizeMake(HScreenWidth * _bannerArray.count,0);
        _pageControl.numberOfPages = _bannerArray.count;
        
        for (int i = 0; i < _bannerArray.count; i++) {
            
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(HScreenWidth * i, 0, HScreenWidth, 130)];
            
            [imgView sd_setImageWithURL:[NSURL URLWithString:bannerArray[i]]];
            imgView.backgroundColor = [UIColor clearColor];
            [_scrollView addSubview:imgView];
        }
        
    }
}

-(void)setThemeArray:(NSArray *)themeArray
{
    if (_themeArray != themeArray) {
        
        _themeArray = themeArray;

        
        [self.button1 setImage:[UIImage imageNamed:@"my_wallet_icon.png"] forState:UIControlStateNormal];
        
        
        UIImageView *imgView1 = [[UIImageView alloc]init];
        [imgView1 sd_setImageWithURL:[NSURL URLWithString:_themeArray[0]]];
        imgView1.frame = _button1.frame;
        imgView1.bottom = _button1.bottom-20;
        [_button1 addSubview:imgView1];
        
        UIImageView *imgView2 = [[UIImageView alloc]init];
        [imgView2 sd_setImageWithURL:[NSURL URLWithString:_themeArray[1]]];
        imgView2.frame = _button2.frame;
        imgView2.bottom = _button2.bottom-20;
        [_button2 addSubview:imgView2];
        
        
        UIImageView *imgView3 = [[UIImageView alloc]init];
        [imgView3 sd_setImageWithURL:[NSURL URLWithString:_themeArray[2]]];
        imgView3.frame = _button3.frame;
        imgView3.bottom = _button3.bottom-20;
        [_button3 addSubview:imgView3];
        
    }
}

-(void)createView
{
    _scrollView.backgroundColor = [UIColor lightGrayColor];
    //回弹效果是否开启
    _scrollView.bounces=NO;
    
    _scrollView.delegate = self;
    //滑动条样式  黑 白  默认
    _scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
    
    //偏移量
    _scrollView.contentOffset=CGPointMake(0, 0);
    
    //是否开启分页滑动
    _scrollView.pagingEnabled=YES;
    
    //是否开启滑动
    _scrollView.scrollEnabled=YES;

    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 100,HScreenWidth, 30)];
    [self addSubview:_pageControl];
  
    //取消自适应
    _pageControl.autoresizingMask = UIViewAutoresizingNone;
    _pageControl.currentPage = 0;

    [_pageControl addTarget:self action:@selector(pageChaned:) forControlEvents:UIControlEventValueChanged];

}

//监听方法
-(void)pageChaned:(UIPageControl *)pageControl{

    [self.timer invalidate];
    
    //滚动
    CGFloat x = pageControl.currentPage * HScreenWidth;
    
    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
    
    [self startTimer]; //开启定时器

}

-(void)startTimer {

    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];

    //循环运行
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
}

-(void)timerAction:(NSTimer *)timer {

    NSUInteger page = (_pageControl.currentPage + 1) % _bannerArray.count;
    
    _pageControl.currentPage = page;
    
    [self pageChaned:_pageControl];
}

#pragma mark - delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取当前偏移量
    CGPoint contentOffset=_scrollView.contentOffset;
    for (int i = 1;i < _bannerArray.count;i++)
    {
        if(contentOffset.x >HScreenWidth / 2 + HScreenWidth *(i-1) && contentOffset.x <HScreenWidth / 2 * 3 + HScreenWidth *(i-1))
        {
            _pageControl.currentPage = i;
        }
        else if (contentOffset.x < HScreenWidth / 2)
        {
            _pageControl.currentPage=0;
        }
    }
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

@end
